import 'package:client_wifi/api/api_server.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wifi_plugin/wifi_plugin.dart';

import 'logic.dart';
import 'state.dart';

/// @description:
/// @author
/// @date: 2025-08-21 17:21:45
class HomePage extends StatelessWidget {
  late final HomeLogic logic = Get.put(HomeLogic(wifi, pwd));
  late final HomeState state = logic.state;

  HomePage({super.key, required this.wifi, required this.pwd});

  final WifiNetwork wifi;
  final String pwd;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await ApiServer().getAllSavedWifi();
        },
      ),
      body: Container(
        child: Obx(() {
          return Center(
            child:
                // state.progressType.value != HomeProgressType.initial ||
                //     state.progressType.value != HomeProgressType.success
                // ? SizedBox(
                //     width: 20,
                //     height: 20,
                //     child: CircularProgressIndicator(strokeAlign: 2),
                //   ):
                GestureDetector(
                  onTap: () async {
                    if (state.progressType.value == HomeProgressType.initial) {
                      await logic.connectToDevice();
                    }
                  },
                  child: Text(
                    getProgressText(),
                    style: TextStyle(fontSize: 20, color: Colors.blue),
                  ),
                ),
          );
        }),
      ),
    );
  }

  String getProgressText() {
    switch (logic.state.progressType.value) {
      case HomeProgressType.scanning:
        return '扫描中';
      case HomeProgressType.connectingDevice:
        return '连接设备中';
      case HomeProgressType.syncWifi:
        return '同步中';
      case HomeProgressType.connecting:
        return '设备连接wifi 中';
      case HomeProgressType.success:
        return '设备已连接WIFI:${state.deviceConnectedWifi.value['ssid']}';
      case HomeProgressType.initial:
        return '连接';
    }
  }
}
